var y = Object.defineProperty;
var v = (l, r, e) =>
  r in l
    ? y(l, r, { enumerable: !0, configurable: !0, writable: !0, value: e })
    : (l[r] = e);
var p = (l, r, e) => (v(l, typeof r != 'symbol' ? r + '' : r, e), e);
import { stringifyCollection as g } from '../stringify/stringifyCollection.js';
import { addPairToJSMap as w } from './addPairToJSMap.js';
import { Collection as M } from './Collection.js';
import { isScalar as d, isPair as c, MAP as k } from './identity.js';
import { Pair as h, createPair as N } from './Pair.js';
import { isScalarValue as P } from './Scalar.js';
function m(l, r) {
  const e = d(r) ? r.value : r;
  for (const t of l)
    if (c(t) && (t.key === r || t.key === e || (d(t.key) && t.key.value === e)))
      return t;
}
class I extends M {
  constructor(e) {
    super(k, e);
    p(this, 'items', []);
  }
  static get tagName() {
    return 'tag:yaml.org,2002:map';
  }
  /**
   * A generic collection parsing method that can be extended
   * to other node classes that inherit from YAMLMap
   */
  static from(e, t, s) {
    const { keepUndefined: i, replacer: o } = s,
      a = new this(e),
      u = (n, f) => {
        if (typeof o == 'function') f = o.call(t, n, f);
        else if (Array.isArray(o) && !o.includes(n)) return;
        (f !== void 0 || i) && a.items.push(N(n, f, s));
      };
    if (t instanceof Map) for (const [n, f] of t) u(n, f);
    else if (t && typeof t == 'object')
      for (const n of Object.keys(t)) u(n, t[n]);
    return (
      typeof e.sortMapEntries == 'function' && a.items.sort(e.sortMapEntries), a
    );
  }
  /**
   * Adds a value to the collection.
   *
   * @param overwrite - If not set `true`, using a key that is already in the
   *   collection will throw. Otherwise, overwrites the previous value.
   */
  add(e, t) {
    var a;
    let s;
    c(e)
      ? (s = e)
      : !e || typeof e != 'object' || !('key' in e)
        ? (s = new h(e, e == null ? void 0 : e.value))
        : (s = new h(e.key, e.value));
    const i = m(this.items, s.key),
      o = (a = this.schema) == null ? void 0 : a.sortMapEntries;
    if (i) {
      if (!t) throw new Error(`Key ${s.key} already set`);
      d(i.value) && P(s.value)
        ? (i.value.value = s.value)
        : (i.value = s.value);
    } else if (o) {
      const u = this.items.findIndex(n => o(s, n) < 0);
      u === -1 ? this.items.push(s) : this.items.splice(u, 0, s);
    } else this.items.push(s);
  }
  delete(e) {
    const t = m(this.items, e);
    return t ? this.items.splice(this.items.indexOf(t), 1).length > 0 : !1;
  }
  get(e, t) {
    const s = m(this.items, e),
      i = s == null ? void 0 : s.value;
    return (!t && d(i) ? i.value : i) ?? void 0;
  }
  has(e) {
    return !!m(this.items, e);
  }
  set(e, t) {
    this.add(new h(e, t), !0);
  }
  /**
   * @param ctx - Conversion context, originally set in Document#toJS()
   * @param {Class} Type - If set, forces the returned collection type
   * @returns Instance of Type, Map, or Object
   */
  toJSON(e, t, s) {
    const i = s
      ? new s()
      : t != null && t.mapAsMap
        ? /* @__PURE__ */ new Map()
        : {};
    t != null && t.onCreate && t.onCreate(i);
    for (const o of this.items) w(t, i, o);
    return i;
  }
  toString(e, t, s) {
    if (!e) return JSON.stringify(this);
    for (const i of this.items)
      if (!c(i))
        throw new Error(
          `Map items must all be pairs; found ${JSON.stringify(i)} instead`,
        );
    return (
      !e.allNullValues &&
        this.hasAllNullValues(!1) &&
        (e = Object.assign({}, e, { allNullValues: !0 })),
      g(this, e, {
        blockItemPrefix: '',
        flowChars: { start: '{', end: '}' },
        itemIndent: e.indent || '',
        onChompKeep: s,
        onComment: t,
      })
    );
  }
}
export { I as YAMLMap, m as findPair };
